package com.wy.dao.daoImpl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.wy.dao.BaseDao;

public class BaseDaoImpl<T, PK extends Serializable> implements BaseDao<T, PK> {

	private SessionFactory sessionFactory;
	@Resource(name="sessionFactory")
	public void setSessionFactory(SessionFactory sessionFactory){
		this.sessionFactory = sessionFactory;
	}
	
	public Session getSession(){
		Session session = null;
		session = sessionFactory.openSession();
		if(session == null){
			System.out.println("session is null...");
		}
		return session;
	}
	
	public boolean add(T t) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		try {
			session.save(t);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			return false;
		} finally{
			session.close();
		}
		return true;
	}

	public boolean update(T t) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		try {
			session.update(t);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			return false;
		} finally{
			session.close();
		}
		return true;
	}

	public T get(PK id) {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean deleteById(Class<T> cz, PK id) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean deleteByIds(Class<T> cz, PK[] ids) {
		// TODO Auto-generated method stub
		return false;
	}

	public Integer getTotalRecord(Class<T> cz) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<T> getListByPage(Integer startRow, Integer pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<T> searchByConditions(Class<T> cz,
			Map<String, Object> conditions) {
		// TODO Auto-generated method stub
		return null;
	}

}
